{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0cb59fcc",
   "metadata": {},
   "source": [
    "--------\n",
    "\n",
    "本学习记录人：  \n",
    "学习时间：2021-11-23  \n",
    "week13  \n",
    "参考的文档：  \n",
    "[python官方文档](https://www.python.org/doc/)  \n",
    "[许老师博客](https://www.cnblogs.com/xuzhichao)  \n",
    "\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03f5df8d",
   "metadata": {},
   "source": [
    "# 1. 列表实践之购物车"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbfa3c89",
   "metadata": {},
   "source": [
    "----\n",
    "任务（25分钟）\n",
    "\n",
    "来源Directory 2 课后习题：\n",
    "\n",
    "　　购物车程序项目：\n",
    "\n",
    "　　要求： 1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "  \n",
    "       2、让用户输入商品编号进行商品的购买。\n",
    "       \n",
    "       3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "       \n",
    "       4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4429df18",
   "metadata": {},
   "source": [
    "----\n",
    "架构：\n",
    "\n",
    "用户输入价格？  \n",
    "商品列表 = [\n",
    "\t\t\t[商品1名称，价格，编号？],\n",
    "\t\t\t[商品2名称，价格，编号？]\n",
    "\t\t]\n",
    "\n",
    "打印\n",
    "-----------\n",
    "商品1 编号1 价格1\n",
    "商品2 编号2 价格2\n",
    "商品3 编号3 价格3\n",
    "\n",
    "用户输入编号购买\n",
    "\n",
    "判断1 商品是否存在？\n",
    "\n",
    "判断2 用户余额是否够？ 够的话扣钱\n",
    "\n",
    "结束 ： 用户输入 “退出” 退出购买（while）\n",
    "\t\t打印用户的购买信息，打印余额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0595cc53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账户余额：10\n",
      "--------商品信息-------\n",
      "商品编号：0 ,商品名称：可乐, 商品价格：3.5\n",
      "商品编号：1 ,商品名称：冰红茶, 商品价格：3\n",
      "商品编号：2 ,商品名称：牛奶, 商品价格：2.5\n",
      "商品编号：3 ,商品名称：柠檬茶, 商品价格：5\n",
      "请输入商品编号：1\n",
      "已经将【冰红茶】加到您的余额还有【7】\n",
      "--------商品信息-------\n",
      "商品编号：0 ,商品名称：可乐, 商品价格：3.5\n",
      "商品编号：1 ,商品名称：冰红茶, 商品价格：3\n",
      "商品编号：2 ,商品名称：牛奶, 商品价格：2.5\n",
      "商品编号：3 ,商品名称：柠檬茶, 商品价格：5\n",
      "请输入商品编号：21656\n",
      "您输入的商品不存在，请输入正确的商品编码（输入‘q’退出购买）：\n",
      "--------商品信息-------\n",
      "商品编号：0 ,商品名称：可乐, 商品价格：3.5\n",
      "商品编号：1 ,商品名称：冰红茶, 商品价格：3\n",
      "商品编号：2 ,商品名称：牛奶, 商品价格：2.5\n",
      "商品编号：3 ,商品名称：柠檬茶, 商品价格：5\n",
      "请输入商品编号：axaca\n",
      "您输入有问题，请重新输入！请输入正确的商品编号（输入‘q’退出购买）\n",
      "--------商品信息-------\n",
      "商品编号：0 ,商品名称：可乐, 商品价格：3.5\n",
      "商品编号：1 ,商品名称：冰红茶, 商品价格：3\n",
      "商品编号：2 ,商品名称：牛奶, 商品价格：2.5\n",
      "商品编号：3 ,商品名称：柠檬茶, 商品价格：5\n",
      "请输入商品编号：2\n",
      "已经将【牛奶】加到您的余额还有【4.5】\n",
      "--------商品信息-------\n",
      "商品编号：0 ,商品名称：可乐, 商品价格：3.5\n",
      "商品编号：1 ,商品名称：冰红茶, 商品价格：3\n",
      "商品编号：2 ,商品名称：牛奶, 商品价格：2.5\n",
      "商品编号：3 ,商品名称：柠檬茶, 商品价格：5\n",
      "请输入商品编号：3\n",
      "您的支付宝余额不足\n",
      "--------商品信息-------\n",
      "商品编号：0 ,商品名称：可乐, 商品价格：3.5\n",
      "商品编号：1 ,商品名称：冰红茶, 商品价格：3\n",
      "商品编号：2 ,商品名称：牛奶, 商品价格：2.5\n",
      "商品编号：3 ,商品名称：柠檬茶, 商品价格：5\n",
      "请输入商品编号：q\n",
      "-----------购物车----------\n",
      "您的购物车为【[['冰红茶', 3], ['牛奶', 2.5]]】,您的余额还有【4.5】\n"
     ]
    }
   ],
   "source": [
    "# 试炼          \n",
    "goods_list =[ \n",
    "    [\"可乐\",3.5],\n",
    "    [\"冰红茶\",3],\n",
    "    [\"牛奶\",2.5],\n",
    "    [\"柠檬茶\",5],\n",
    "    \n",
    "]\n",
    "\n",
    "shopping_cart = []\n",
    "\n",
    "Alipay = int(input('请输入您的账户余额：'))\n",
    "\n",
    "\n",
    "while True:\n",
    "    print(\"--------商品信息-------\")\n",
    "    for i in range(len(goods_list)):\n",
    "              print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    chioce = input(\"请输入商品编号：\")\n",
    "    \n",
    "    if chioce.isdigit():\n",
    "        if int(chioce)>=0 and int(chioce)<len(goods_list):\n",
    "            if Alipay >= goods_list[int(chioce)][1]:\n",
    "                Alipay=Alipay-goods_list[int(chioce)][1]\n",
    "                shopping_cart.append(goods_list[int(chioce)])\n",
    "                print(\"已经将【%s】加到您的余额还有【%s】\"%(goods_list[int(chioce)][0],Alipay))\n",
    "            else:\n",
    "                print(\"您的支付宝余额不足\")\n",
    "        else:\n",
    "            print(\"您输入的商品不存在，请输入正确的商品编码（输入‘q’退出购买）：\")\n",
    "            \n",
    "      \n",
    "    elif chioce == \"q\":    \n",
    "        print(\"-----------购物车----------\")\n",
    "        print(\"您的购物车为【%s】,您的余额还有【%s】\"%(shopping_cart,Alipay))\n",
    "        break    \n",
    "    \n",
    "    else:\n",
    "         print(\"您输入有问题，请重新输入！请输入正确的商品编号（输入‘q’退出购买）\")\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "73b9b280",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账户余额：2\n",
      "--------商品信息-------\n",
      "商品编号：0 ,商品名称：珍珠奶茶小杯, 商品价格：6\n",
      "商品编号：1 ,商品名称：椰果奶茶中杯, 商品价格：8\n",
      "商品编号：2 ,商品名称：芝士奶盖大杯, 商品价格：7\n",
      "商品编号：3 ,商品名称：芋圆啵啵奶茶, 商品价格：14\n",
      "商品编号：4 ,商品名称：杨枝甘露, 商品价格：8\n",
      "请输入商品编号：1\n",
      "您的支付宝余额不足\n",
      "--------商品信息-------\n",
      "商品编号：0 ,商品名称：珍珠奶茶小杯, 商品价格：6\n",
      "商品编号：1 ,商品名称：椰果奶茶中杯, 商品价格：8\n",
      "商品编号：2 ,商品名称：芝士奶盖大杯, 商品价格：7\n",
      "商品编号：3 ,商品名称：芋圆啵啵奶茶, 商品价格：14\n",
      "商品编号：4 ,商品名称：杨枝甘露, 商品价格：8\n",
      "请输入商品编号：q\n",
      "-----------购物车----------\n",
      "您的购物车为【[]】,您的余额还有【2】\n"
     ]
    }
   ],
   "source": [
    "### 1.商品信息\n",
    "goods_list = [\n",
    "    ['珍珠奶茶小杯',6],\n",
    "    ['椰果奶茶中杯',8],\n",
    "    ['芝士奶盖大杯',7],\n",
    "    ['芋圆啵啵奶茶',14],\n",
    "    ['杨枝甘露',8]\n",
    "]\n",
    "### 要添加用户的商品列表：\n",
    "shopping_cart=[]\n",
    "\n",
    "# 2.用户输入\n",
    "Alipay = int(input('请输入您的账户余额：'))\n",
    "\n",
    "# 3.打印结果：商品编号：%s ,商品名称：%s, 商品价格：%s\n",
    "# goods_list[0][0] 商品名称 goods_list[0][1] 商品价格\n",
    "\n",
    "\n",
    "# 占位符 %s  s--->str\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(0,goods_list[0][0],goods_list[0][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(1,goods_list[1][0],goods_list[1][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(2,goods_list[2][0],goods_list[2][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(3,goods_list[3][0],goods_list[3][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(4,goods_list[4][0],goods_list[4][1]))\n",
    "\n",
    "# print(\"商品编号：\",0,\" ,商品名称：\",goods_list[0][0],\", 商品价格：\",goods_list[0][1])\n",
    "# print(\"商品编号：\",1,\" ,商品名称：\",goods_list[1][0],\", 商品价格：\",goods_list[1][1])\n",
    "# print(\"商品编号：\",2,\" ,商品名称：\",goods_list[2][0],\", 商品价格：\",goods_list[2][1])\n",
    "# print(\"商品编号：\",3,\" ,商品名称：\",goods_list[3][0],\", 商品价格：\",goods_list[3][1])\n",
    "# print(\"商品编号：\",4,\" ,商品名称: \",goods_list[4][0],\", 商品价格：\",goods_list[4][1])\n",
    "\n",
    "# 思考：假如有一万件商品？怎么办？\n",
    "# 请使用 for+range（）  实现上述5行代码的结果\n",
    "# 思考： 如果增加商品了，或者下架了商品 5是不是变了\n",
    "while True:\n",
    "    print(\"--------商品信息-------\")\n",
    "    for i in range(len(goods_list)):\n",
    "          print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    # 4.让用户输入商品编号进行商品的购买\n",
    "    chioce = input(\"请输入商品编号：\")# input() 默认为字符串\n",
    "    # Alipay VS 用户选择商品的价格\n",
    " \n",
    "    if chioce.isdigit():\n",
    "        \n",
    "        # 一、用户输入的是编号\n",
    "        # ①. 编号在列表范围内\n",
    "        \n",
    "        if int(chioce)>=0 and int(chioce)<len(goods_list):\n",
    "            # ------------------------------------------------------------------------\n",
    "            # 1. 钱包够，扣钱，给Alipay赋值扣完之后的金额 2. 钱不够，提示用户\n",
    "            # goods_list[choice][1] ----> 用户选择的商品的 价格\n",
    "            # goods_list[choice] = ['椰果奶茶中杯', 7]\n",
    "            if Alipay >= goods_list[int(chioce)][1]:\n",
    "                Alipay=Alipay-goods_list[int(chioce)][1]\n",
    "                shopping_cart.append(goods_list[int(chioce)])\n",
    "                print(\"已经将【%s】加到您的余额还有【%s】\"%(goods_list[int(chioce)][0],Alipay))\n",
    "            else:\n",
    "                print(\"您的支付宝余额不足\")\n",
    "        else:\n",
    "             # ②. 编号不在列表范围内\n",
    "            print(\"您输入的商品不存在，请输入正确的商品编码（输入‘q’退出购买）：\")\n",
    "    \n",
    "    elif chioce == \"q\":    \n",
    "         # 二、用户输入 ‘q’ 退出\n",
    "        print(\"-----------购物车----------\")\n",
    "        print(\"您的购物车为【%s】,您的余额还有【%s】\"%(shopping_cart,Alipay))\n",
    "        break    \n",
    "    else:\n",
    "       # 三、用户输入什么都不是\n",
    "         print(\"您输入有问题，请重新输入！请输入正确的商品编号（输入‘q’退出购买）\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3d6491a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(goods_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc24e2c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考： 如果增加商品了，或者下架了商品 5是不是变了\n",
    "for i in range(len(good_list)):\n",
    "    print(i,goods_list[i][0],goods_list[i][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d09becc8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d043743",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 商品信息：列表数据\n",
    "# goods_list[int(choice)] 用户输入之后选择的商品信息\n",
    "goods_list = [\n",
    "    ['珍珠奶茶小杯',6],\n",
    "    ['椰果奶茶中杯',8],\n",
    "    ['杨枝甘露',7],\n",
    "    ['圣代',6],\n",
    "    ['多肉葡萄',10]\n",
    "]\n",
    "\n",
    "# 用户购物车信息：列表数据\n",
    "user_shopping_cart = []\n",
    "\n",
    "# 用户输入\n",
    "Alipay = int(input('请输入您的账户余额：'))\n",
    "\n",
    "# 打印商品信息\n",
    "# ---------\n",
    "# 商品编号：%s ,商品名称：%s, 商品价格：%s\n",
    "# 商品编号：%s ,商品名称：%s, 商品价格：%s\n",
    "\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(0,goods_list[0][0],goods_list[0][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(1,goods_list[1][0],goods_list[1][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(2,goods_list[2][0],goods_list[2][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(3,goods_list[3][0],goods_list[3][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(4,goods_list[4][0],goods_list[4][1]))\n",
    "while True: # 考虑什么时候退出\n",
    "    print(\"-----------商品列表----------\")\n",
    "    for i in range(len(goods_list)):\n",
    "        print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    choice = input(\"请输入你要购买的商品编号（输入‘q’退出购买）：\") # 1. 输入编号数字  2. 输入“q”3. 输入的啥也不是！\n",
    "    if choice.isdigit():\n",
    "        if int(choice)>=0 and int(choice)<len(goods_list):\n",
    "        # 一、数字 在 列表范围内\n",
    "            # 1. 输入编号数字 数值应该在 商品列表范围以内 \n",
    "            # 判断1 ： Alipay  VS  goods_list[choice][1]（商品价格）   goods_list[int(choice)] (用户输入之后选择的商品信息)\n",
    "            if Alipay >= goods_list[int(choice)][1]:\n",
    "                # 1. Alipay重新赋值\n",
    "                Alipay = Alipay - goods_list[int(choice)][1] # 扣钱\n",
    "                print(\"\\n你点的【%s】已经购买成功，请等候~，您的余额还有【%s】元\\n\"%(goods_list[int(choice)][0],Alipay))\n",
    "                user_shopping_cart.append(goods_list[int(choice)] ) # 增加用户选择的商品信息\n",
    "            else:\n",
    "                print(\"\\n你的钱包不够啊，换种方式支付吧~\\n\")\n",
    "        else:\n",
    "        # 二、数字 不在 列表范围内\n",
    "            print(\"您输入的商品不存在，请输入正确的商品编码（输入‘q’退出购买）：\")\n",
    "    elif choice == \"q\":\n",
    "        # 2. 输入“q”\n",
    "        print(\"-----------购物车----------\")\n",
    "        print(\"您的购物车为【%s】,您的余额还有【%s】\"%(user_shopping_cart,Alipay))\n",
    "        break    \n",
    "    else:\n",
    "        # 3.  输入的啥也不是！不符合要求\n",
    "        print(\"**您输入的内容不符合要求，请检查重新输入！**\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "025033d0",
   "metadata": {},
   "source": [
    "# 2. 请同学们尝试完成实践（二）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "cf74bec7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:········\n",
      "guess_digit:1\n",
      "think bigger...\n",
      "guess_digit:3\n",
      "think bigger...\n",
      "guess_digit:11\n",
      "think smaller...\n",
      "guess_digit:1\n",
      "think bigger...\n",
      "guess_digit:\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: ''",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-41-5c57671ebf68>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     11\u001b[0m     \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mcount\u001b[0m \u001b[1;33m<\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m             \u001b[0mguess_digit\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"guess_digit:\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     14\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mguess_digit\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mhide_card\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m                 \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"yes, you got it.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: ''"
     ]
    }
   ],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "# Author:Zhichao\n",
    "#猜数字（0-100）\n",
    "\n",
    "\n",
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card:\"))\n",
    "count = 0\n",
    "\n",
    "if hide_card <100 and hide_card >0:\n",
    "    while True:\n",
    "        if count <5:\n",
    "            guess_digit = int(input(\"guess_digit:\"))\n",
    "            if guess_digit == hide_card:\n",
    "                print(\"yes, you got it.\")\n",
    "                break\n",
    "            elif guess_digit > hide_card:\n",
    "                print(\"think smaller...\")\n",
    "            else:\n",
    "                print(\"think bigger...\")\n",
    "            count +=1\n",
    "        else:\n",
    "            print(\"猜这么多次都不对,你个笨蛋!\")\n",
    "            print(\"正确答案：%s\"%(hide_card))\n",
    "            break\n",
    "else:\n",
    "    print(\"请输入数字在0-99之间。不要耍赖！\")1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "509f5ca4",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "Interrupted by user",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-43-e079bb09ee58>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mgetpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mhide_card\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgetpass\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetpass\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hide_card\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mcount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mhide_card\u001b[0m\u001b[1;33m<\u001b[0m\u001b[1;36m100\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mhide_card\u001b[0m \u001b[1;33m>\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36mgetpass\u001b[1;34m(self, prompt, stream)\u001b[0m\n\u001b[0;32m    841\u001b[0m             warnings.warn(\"The `stream` parameter of `getpass.getpass` will have no effect when using ipykernel\",\n\u001b[0;32m    842\u001b[0m                     UserWarning, stacklevel=2)\n\u001b[1;32m--> 843\u001b[1;33m         return self._input_request(prompt,\n\u001b[0m\u001b[0;32m    844\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    845\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m    902\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    903\u001b[0m                 \u001b[1;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 904\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Interrupted by user\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    905\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    906\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid Message:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card\"))\n",
    "count = 0\n",
    "\n",
    "if hide_card<100 and hide_card >0:\n",
    "    while True:\n",
    "        if count<5:\n",
    "            guess_digit = int(input(\"guess_digit:\"))\n",
    "            if guess_digit ==hide_card:\n",
    "                print(\"yes,you got it.\")\n",
    "                break\n",
    "            elif guess_digit > hide_card:\n",
    "                print(\"think samller...\")\n",
    "            else:\n",
    "                print(\"think bigger...\")\n",
    "            count +=1\n",
    "        else:\n",
    "            print(\"猜错啦~\")\n",
    "            print(\"正确答案：%s\"%(hide_card))\n",
    "            break\n",
    "else:\n",
    "    print(\"请输入在0-99之间的数字哦\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "35259c8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:········\n",
      "guess_digit:1\n",
      "think bigger...\n",
      "guess_digit:11\n",
      "think smaller...\n",
      "guess_digit:1\n",
      "think bigger...\n",
      "guess_digit:\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: ''",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-42-5c57671ebf68>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     11\u001b[0m     \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mcount\u001b[0m \u001b[1;33m<\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m             \u001b[0mguess_digit\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"guess_digit:\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     14\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mguess_digit\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mhide_card\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m                 \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"yes, you got it.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: ''"
     ]
    }
   ],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "# Author:Zhichao\n",
    "#猜数字（0-100）\n",
    "\n",
    "\n",
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card:\"))\n",
    "count = 0\n",
    "\n",
    "if hide_card <100 and hide_card >0:\n",
    "    while True:\n",
    "        if count <5:\n",
    "            guess_digit = int(input(\"guess_digit:\"))\n",
    "            if guess_digit == hide_card:\n",
    "                print(\"yes, you got it.\")\n",
    "                break\n",
    "            elif guess_digit > hide_card:\n",
    "                print(\"think smaller...\")\n",
    "            else:\n",
    "                print(\"think bigger...\")\n",
    "            count +=1\n",
    "        else:\n",
    "            print(\"猜这么多次都不对,你个笨蛋!\")\n",
    "            print(\"正确答案：%s\"%(hide_card))\n",
    "            break\n",
    "else:\n",
    "    print(\"请输入数字在0-99之间。不要耍赖！\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
